package com.synnex.mobile.servier.web.common.security.admin;

import com.synnex.mobile.generic.core.json.JSONModel;
import com.synnex.mobile.generic.web.session.WebSession;
import com.synnex.mobile.servier.web.common.security.CommonGroup;
import com.synnex.mobile.servier.web.common.session.admin.AdminSession;
import com.synnex.mobile.servier.web.common.session.mobile.MobileWebSession;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 *
 * @author Lance Chen
 */
public class AdminGroup extends CommonGroup {

    private static final long serialVersionUID = 1L;

    @Override
    public boolean request(HttpServletRequest request, HttpServletResponse response) {
        boolean pass = super.request(request, response);
        if (pass) {
            return pass;
        } else {
            //if user is setting to web session
            AdminSession webSession = (AdminSession) getSession(request);
            boolean isLogin = webSession.getUser() != null;
            if (isLogin) {
                return true;
            } else {
                try {
                    response.sendRedirect(request.getContextPath() + failPage);
                    return false;
                } catch (IOException ex) {
                    logger.error(ex.getMessage(), ex);
                    return false;
                }
            }
        }
    }

    @Override
    protected AdminSession getSession(HttpServletRequest request) {
        return WebSession.getCurrentSession(AdminSession.class, request);
    }

    @Override
    protected AdminSession createSession(HttpServletRequest request) {
        return WebSession.create(AdminSession.class, request);
    }
}
